<?php

class GoodController extends CommonController{
	
	private $_model;
	
	public function __init(){
		
		$this->_model=K('good');
	}
	
	public function index(){
		
		$data=$this->_model->get_all_data();
		
		$this->assign('data',$data);
		
		$this->display();
	}
	
	public function add(){
		
		if(IS_POST){
			
			if(!$this->_model->add_data()) $this->error($this->_model->error);
			
			$this->success('添加成功');
		}
		
		$cate=K('category')->get_all_data();
		$this->assign('cate',$cate);
		
		$brand=K('brand')->get_all_data();
		$this->assign('brand',$brand);
		
		$this->display();
	}
	
	 public function upload()

    {

        $upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();

        if (empty($file)) {

            $this->ajax('上传失败');

        } else {
			
            $data = $file[0];
//			serialize($value)
//Upload/Content/15/01/11051421411189_sthumb.jpg 
			$newPath = str_replace('.', '_sthumb.', $data['path']);
			$img = new image(); 
			$data[0]=$img->thumb($data['path'],$newPath,100,100,2);
			$newPath = str_replace('.', '_mthumb.', $data['path']);
			$data[1]=$img->thumb($data['path'],$newPath,450,680,2);
		
			
            $this->ajax($data);

        }

    }
	
	public function ajax_get_cate(){
		if(!IS_AJAX) $this->error('非法请求');
		
		$cid = (Q('post.cid'));
		
		$data = K('category')->get_tid(array('cid'=>$cid));
		
		$data=K('tproperty')->get_all_data(array('tid'=>$data[0]));
//		p($data);

		foreach ($data as $k => $v) {
			$data[$k]['tpvalue']=explode(',', $data[$k]['tpvalue']);
		}
//		p($data);
		$this->ajax($data);
	}
	
	
	public function edit(){
		
		$where=array('gid'=>Q('get.gid'));
		
		if(IS_POST){
			if(!$this->_model->edit_data($where)) $this->error($this->_model->error);
			
			$this->success('修改成功');
		}
		
			$data=$this->_model->get_one_data($where);
			
			
			$this->assign('data',$data);
			$cate=K('Category')->get_all_data();
			
			$this->assign('cate',$cate);
			
			$brand=K('Brand')->get_all_data();
			$this->assign('brand',$brand);
			
			$gdata=K('Gdata')->get_one_data($where);
			$this->assign('gdata',$gdata);
			
			$gproperty=K('Gproperty')->get_all_data($where);
			$this->assign('gproperty',$gproperty);
			
			$gt = K('GTpropertyView')->get_all_data();
			
//			p($gt);die;	
			
			$this->display();
	}
	
	public function del(){
		
		$where=array('gid'=>Q('get.gid'));
		$this->_model->del_data($where);
//		p($where);die;
		$this->success('删除成功');
	}
	
}








